package dao.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import dao.tHistoryDao;
import entity.tHistory;

public class tHistoryDaoImpl extends HibernateDaoSupport implements tHistoryDao {

	public void add(String sql) {
		try {
			System.out.println("sql:"+sql);
			Session session = getHibernateTemplate().getSessionFactory()
					.openSession();
			System.out.println("----------");
			Transaction tx = session.beginTransaction();
			System.out.println("----------");
			SQLQuery query = session.createSQLQuery(sql);
			System.out.println("----------");
			query.executeUpdate();
			System.out.println("----------");
			tx.commit();
		} catch (HibernateException e) {
			throw e;
		}

	}

	public void delete() {
		// TODO Auto-generated method stub

		tHistory thistory = new tHistory();

		thistory.setId("111");
		this.getHibernateTemplate().delete(thistory);

	}

	public List<tHistory> search() {
		// TODO Auto-generated method stub

		String hql = "from tHistory";

		return this.getHibernateTemplate().find(hql);

	}

	public void update() {
		// TODO Auto-generated method stub

		tHistory thistory = new tHistory();
		Date date = new Date();
		Timestamp ts = new Timestamp(date.getTime());

		thistory.setId("111");
		thistory.sethData("a34a");
		thistory.setMark(32452);

		thistory.sethTime(ts);

		this.getHibernateTemplate().update(thistory);

	}

}
